package com.wei.czz.framework.blog.controller;

import com.wei.czz.common.annotation.Auth;
import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.blog.audit.group.BlogAuditGroupDto;
import com.wei.czz.common.dto.blog.audit.group.BlogAuditGroupPageEnumDto;
import com.wei.czz.common.jsr303.group.Save;
import com.wei.czz.common.jsr303.group.Search;
import com.wei.czz.common.jsr303.group.Update;
import com.wei.czz.common.property.BatchProperty;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.blog.audit.group.BlogAuditGroupFormVo;
import com.wei.czz.common.vo.blog.audit.group.BlogAuditGroupVo;
import com.wei.czz.framework.blog.manager.BlogAuditGroupManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-12-05 08:21:03
 * className: BlogAuditGroupController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/blog/audit/group")
@AllArgsConstructor
public class BlogAuditGroupController {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditGroupController.class);

    private final BatchProperty batchProperty;

    private final BlogAuditGroupManager blogAuditGroupManager;

    /**
     * 保存博客审核分组接口
     * <p>
     * 调用方：
     *  1、浏览器-后台管理页面-博客审核分组管理页面-博客审核分组新增/修改弹出框中调用
     * <p>
     * 此接口会校验是否重复提交，重复提交限制为审核类型和审核级别
     * @param vo 参数对象
     * @return 结果
     */
    @Resubmit(
            spEL = "'saveBlogAuditGroup' + #vo.auditType + '-' + #vo.auditLevel"
            , message = "保存博客审核分组"
            , resubmitTime = 1
            , resubmitUnit = TimeUnit.MINUTES
    )
    @PostMapping("/save")
    @Auth(permit = "blog:audit:group:save")
    public Result<Object> saveBlogAuditGroup(@Validated(Save.class) @RequestBody BlogAuditGroupVo vo) {
        log.info("开始保存博客审核分组业务。{}", vo);

        blogAuditGroupManager.saveBlogAuditGroup(vo);

        log.info("保存博客审核分组业务结束");
        return Result.success();
    }

    /**
     * 分页获取博客审核分组接口
     * <p>
     * 调用方：
     *  1、浏览器-后台管理页面-博客审核分组管理页面中调用
     * @param formVo 表单对象
     * @return 结果
     */
    @PostMapping("/page/list")
    @Auth(permit = "blog:audit:group:list")
    public Result<PageDto<BlogAuditGroupDto>> getBlogAuditGroupPageList(
            @Validated(Search.class) @RequestBody BlogAuditGroupFormVo formVo) {
        log.info("开始分页获取博客审核分组业务。{}", formVo);

        PageDto<BlogAuditGroupDto> pageDto = blogAuditGroupManager.getBlogAuditGroupPageList(formVo);

        log.info("分页获取博客审核分组业务结束");
        return Result.success(pageDto);
    }

    /**
     * 获取博客审核分组管理页面枚举接口
     * <p>
     * 调用方：
     *  1、浏览器-后台管理页面-博客审核分组管理页面中调用
     * @return 结果
     */
    @GetMapping("/page/enum")
    @Auth(permit = "blog:audit:group:list")
    public Result<BlogAuditGroupPageEnumDto> getBlogAuditGroupPageEnum() {
        log.info("开始获取博客审核分组管理页面枚举业务");

        BlogAuditGroupPageEnumDto blogAuditGroupPageEnumDto = blogAuditGroupManager.getBlogAuditGroupPageEnum();

        log.info("获取博客审核分组管理页面枚举业务结束");
        return Result.success(blogAuditGroupPageEnumDto);
    }

    /**
     * 修改博客审核分组接口
     * @param vo 参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'updateBlogAuditGroup' + #userId", message = "修改博客审核分组")
    @PostMapping("/update")
    @Auth(permit = "blog:audit:group:update")
    public Result<Object> updateBlogAuditGroup(@Validated(Update.class) @RequestBody BlogAuditGroupVo vo) {
        log.info("开始修改博客审核分组业务。{}", vo);

        blogAuditGroupManager.updateBlogAuditGroup(vo);

        log.info("修改博客审核分组业务结束");
        return Result.success();
    }

    /**
     * 删除博客审核分组接口
     * @param idList 主键列表
     * @return 结果
     */
    @Resubmit(spEL = "'deleteBlogAuditGroup' + #userId", message = "删除博客审核分组")
    @DeleteMapping("/delete")
    @Auth(permit = "blog:audit:group:delete")
    public Result<Object> deleteBlogAuditGroup(@RequestBody List<Long> idList) {
        log.info("开始删除博客审核分组业务。idList={}", idList);

        if (CollectionUtils.isEmpty(idList)) {
            return Result.clientFail("主键列表不能为空");
        }
        if (batchProperty.getMaxSize() < idList.size()) {
            log.warn("删除博客审核分组数量超限。batch.maxSize={} list.size={}", batchProperty.getMaxSize(), idList.size());
            return Result.clientFail("一次删除博客审核分组最多可以同时" + batchProperty.getMaxSize() + "条数据");
        }

        blogAuditGroupManager.deleteBlogAuditGroup(idList);

        log.info("删除博客审核分组业务结束");
        return Result.success();
    }
}
